<?php

namespace app\activity\model;

use app\BaseModel;
use think\facade\Db;

/**
 * @mixin think\Model
 */
class Activity extends BaseModel
{
    protected $name = 'lb_activity_activity';
    protected $pk = 'id';


    /**
     * @author chenniang
     * @DataTime: 2020-06-17 14:46
     * @功能说明:转格式
     */
    public function getContentTextAttr($value,$data){

        if(!empty($value)){

            return  html_entity_decode($value);
           // return  getimgsV2($value);

        }else{

            return '';
        }

    }


    public function getListByFilter($filter, $page_config = ['page' => 1, 'page_count' => 20])
    {
        return $this
            ->where($filter)
            ->with('activityItems')
            ->page($page_config['page'], $page_config['page_count'])->select()->toArray();
    }

    public function countByFilter($filter)
    {
        $result = Db::name($this->name)
            ->where($filter)
            ->count();

        return $result;
    }

    public function createOne($data)
    {
        $time = time();
        $data['create_time'] = $time;
        $data['update_time'] = $time;
        $res = $this->strict(false)->insert($data, true);
        return $res;
    }

    public function updateByfilter($where, $data)
    {
        $data['update_time'] = time();
        $res = $this->strict(false)->where($where)->update($data);
        return $res;
    }

    public function getOne($where)
    {
        $data = $this->where($where)->with('activityItems')->find();
        return !empty($data) ? $data->toArray() : $data;
    }

    public function activityItems()
    {
        return $this->hasMany(ActivityItemsInActivity::class, 'activity_id', 'id');
    }

    public function returnSquarePoint($lng, $lat,$distance = 5) {
        $dlng =  2 * asin(sin($distance / (2 * 6371)) / cos(deg2rad($lat)));
        $dlng = rad2deg($dlng);
        $dlat = $distance/6371;
        $dlat = rad2deg($dlat);

        return array(
            'left-top'=>array('lat'=>$lat + $dlat,'lng'=>$lng-$dlng),
            'right-top'=>array('lat'=>$lat + $dlat, 'lng'=>$lng + $dlng),
            'left-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng - $dlng),
            'right-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng + $dlng)
        );

    }

}
